﻿using System;
using System.Data;
using MailClient;
using MailClient.Data;
using MailClient.Handlers;

namespace MailClient.Commands
{
    /// <summary>
    /// Similar to "LIST" but gives less detail
    /// </summary>
    class info : Query, ICommand, IDisposable
    {
        private Handlers.designator _designator;
        public Handlers.designator Designator
        {
            get { return _designator; }
            set { _designator = Designator; }
        }
        public info(Command com, string[] query) { _query = query; }

        private string[] _query;


        private MailMessage _msg;

        public MailMessage MsgReply
        {
            get { return _msg; }
        }

        public void Dispose() { }

        public bool CommitData() { return false; }

        public bool Command(Job job)
        {
            string preamble = "";
            string table = @"<TABLE border=""0"" cellpadding=""1"">
                                <TR>
                                    <TD bgcolor=""#FFFFFF"" align=""center"" width=""500"" valign=""middle"">
                                        <TABLE border=""0"" cols=""2"" style=""font-size: 9pt; color: black; font-family: verdana, helvetica"" width=""100%"" cellspacing=""2"" cellpadding=""0"">";

            foreach (string s in _query)
            {

                DataRow[] rows = TryQuery(job.GetJobType(s), s);
                foreach (DataRow row in rows)
                {
                    #region Job Person
                    if (job.GetJobType(s) == JobType.Person)
                    {
                        string[] fields = { "first_name", "last_name", "job_title", "direct_phone", "mobile_phone", "email" };

                        foreach (string col in fields)
                        {
                            table += CreateHtmlTableRow(row, col);
                        }
                        fields[0] = row["company_id"].ToString();
                        DataRow[] JobRows = CrossMap("companies", "company_id=" + "'" + fields[0] + "'");

                        foreach (DataRow JobRow in JobRows)
                        {
                            string[] company_fields = { "company_name", "street", "suite", "city", "state", "zip", "main_phone", "fax" };

                            foreach (string col in company_fields)
                            {
                                table += CreateHtmlTableRow(JobRow, col);
                            }
                        }
                    }
                    #endregion
                    #region Job Store
                    else if (job.GetJobType(s) == JobType.Store)
                    {
                        string[] fields = { "str_num", "str_city", "str_st" };

                        foreach (string col in fields)
                        {
                            table += CreateHtmlTableRow(row, col);
                        }

                        string[] Tridates = { "str_num", "str_seq", "city", "st", "pwo_prj", "otp_prj", "arch_permit_prj" };
                        string qr = "str_num= '" + row["str_num"].ToString() + "'";
                        qr += " AND ";
                        qr += "str_seq= '" + row["str_seq"].ToString() + "'";
                        DataRow[] dates = ScheduleData.Tables["trident"].Select(qr);
                        foreach (DataRow date in dates)
                        {
                            foreach (string col in Tridates)
                            {
                                table += CreateHtmlTableRow(date, col);

                            }
                        }
                    }
                    #endregion
                    table += "<TR>&nbsp;</TR>";
                }
            }
            preamble = "Information here is accurate as we can make it.<br />Please reply or notify us if you believe something is incorrect<br />";

            table += "</TABLE></TD></TR></TABLE>";



            _msg = new MailMessage();
            _msg.Subject = string.Format("RE: info for {0}", job.TestString);
            _msg.Body = preamble + table;
            _msg.IsBodyHtml = true;
            var from = new System.Net.Mail.MailAddress("rha@rhaaia.com", "RHA Email");
            _msg.To.Add(job.ReplyTo);
            _msg.Sender = from;
            return true;
        }
    }
}